#! -*- coding: utf-8 -*-
import os
from datetime import datetime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine
from sqlalchemy import Column, Integer, String, schema, Boolean, Enum, DateTime, Text, ForeignKey, LargeBinary


engine = create_engine(os.environ['DATABASE_URL'])

Base = declarative_base()


        
class Book(Base):
    """图书信息"""
    __tablename__ = 'book'

    # 必须字段
    Id = Column(Integer, primary_key=True, autoincrement='auto')
    name = Column(String(200), nullable=False, comment='书名')
    author = Column(String(32), nullable=False, comment='作者')
    showUrl = Column(String(200), nullable=False, comment='书籍的有效地址')
    describe = Column(Text, default='此书暂无描述', comment='书籍描述')
    booktype = Column(String(200), default='未分类', comment='书籍分类标签')
    # 可为空
    downUrl = Column(String(200), comment='下载地址')
    wordNum = Column(Integer, default=-1, comment='总字数，-1表示未知')
    state = Column(Enum('notnow', 'Eunuch', 'end', 'noend'), default='notnow', comment='书籍状态')

class BookInfo(Base):
    '''图书详细信息'''
    __tablename__ = 'bookinfo'

    InfoId = Column(Integer, ForeignKey('book.Id'), primary_key=True, comment='书籍id')
    collectnum = Column(Integer, default=0, comment='收藏数')
    readnum = Column(Integer, default=0, comment='点击阅读数')
    downnum = Column(Integer, default=0, comment='下载次数')

        
class User(Base):
    '''用户信息管理'''
    __tablename__ = 'user'

    Id = Column(Integer, primary_key=True, autoincrement='auto')
    username = Column(String(32), nullable=False, comment='用户名')
    password = Column(String(64), nullable=False, comment='密码')
    email = Column(String(100), default=None, comment='邮箱')

class UserInfo(Base):
    '''用户详细信息'''
    __tablename__ = 'userinfo'

    InfoId = Column(Integer, ForeignKey('user.Id'), primary_key=True)
    permission = Column(Enum('0', '1', '2', '3', '4', '5'), default=1, comment='用户权限，1表示普通，2表示vip，4表示管理员，5表示最高管理员，0表示账号不可用, 3保留')
    avatar = Column(Text, default=None, comment='头像')
    registDate = Column(DateTime, default=datetime.now(), comment='注册日期')
    loginDate = Column(DateTime, default=datetime.now(), comment='上次登录时间')
    collect = Column(Text, default='', comment='收藏的书籍，书籍id用|间隔出来')
    down = Column(Text, default='', comment='下载过的数据，同样用|间隔')
    noShow = Column(Text, default='', comment='用户标记不要再看到的书籍')
    state = Column(Enum('在线', '离线', '注销'), default='离线', comment='用户登录状态')
    remarks = Column(Text, default='', comment='备注，可以个性化扩展一些用户信息')


        
    
Base.metadata.create_all(engine)
